<?php


namespace app\meetyou\controller;

use app\common\service\MemberService;
use library\Controller;
use library\tools\Data;
use PDO;
use think\Db;

/**
 * 会员信息管理
 * Class Member
 * @package app\meetyou\controller
 */
class Member extends Controller {
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'meetyou_member';

    /**
     * 会员信息管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index() {
        $this->title = '会员信息管理';

        $this->level_list = Db::table('meetyou_member_level')->order('level')->column('id,level,levelname');
        $name = '%' . input('name', '') . '%';

        $query = $this->_query($this->table)->alias('m')
            ->leftJoin(' meetyou_member p ', 'p.id=m.parent_id')
            ->leftJoin('meetyou_member_level l', 'l.level = m.level')
            ->field('m.*,p.id as pid,p.nickname as pnickname,p.phone as pphone,l.levelname')
            ->equal('m.id#id,m.level#level');
        if ($name !== '%%') {
            $query->where('m.account LIKE ? OR m.nickname LIKE ? OR m.phone LIKE ? OR m.realname LIKE ?', [$name, $name, $name, $name]);
        }
        $query->dateBetween('m.create_at#create_at')
            ->order('m.create_at desc')->page();
    }

    /**
     * 添加会员信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add() {
        $this->title = '添加会员信息';
        $this->applyCsrfToken();
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑会员信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit() {
        $this->title = '编辑会员信息';
        $this->applyCsrfToken();
        $this->_form($this->table, 'form');
    }

    /**
     * 会员表单处理
     * @param array $vo
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_filter(&$vo) {
        if ($this->request->isPost()) {
            if (empty($vo['id'])) $vo['id'] = Data::uniqidNumberCode(10);
            if (isset($vo['create_at']) && empty($vo['create_at'])) $vo['create_at'] = time();
            if (isset($vo['avatar']) && empty($vo['avatar'])) $vo['avatar'] = '/static/img/avatar.png';
            $vo['update_at'] = time();
            if (isset($vo['account'])) {
                $account = trim($vo['account']);
                $id = isset($vo['id']) ? $vo['id'] : 0;
                if (db($this->table)->where('account', $account)->where('id', '<>', $id)->find()) {
                    $this->error('用户名不能重复');
                }
            }
        } else {
            $this->level_list = Db::table('meetyou_member_level')->order('level')->column('id,level,levelname');
        }
    }

    /**
     * 会员积分充值
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function recharge() {
        $this->title = '会员积分充值';
        $this->applyCsrfToken();
        if ($this->request->isPost()) {
            $post = $this->request->post();
            //$rechare_type = intval($post['rechare_type']);
            $type = 'credit1';
            $mid = intval($post['id']);
            $credit = db($this->table)->where('id', $mid)->value('credit1');
            $num = floatval($post['num']);

            if ($num <= 0) {
                $this->error('请填写大于0的数字！', '');
            }

            $remark = trim($post['remark']);
            if (empty($remark)) $remark = '后台会员充值';
            $changetype = intval($post['rechare_type']);
            if ($changetype == 0) {
                $num -= $credit;
            } else {
                if ($changetype == -1) {
                    $num = -$num;
                }
            }

            if (MemberService::recharge($mid, $num, $remark))
                $this->success('会员积分修改成功！', '');
            else
                $this->error('充值数据有误');
        } else {
            $this->_form($this->table, 'recharge');
        }
    }


    /**
     * 删除会员信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove() {
        $this->applyCsrfToken();
        $this->_delete($this->table);
    }

    /**
     * 表单结果处理
     * @param boolean $result
     */
    protected function _form_result($result) {
        if ($result && $this->request->isPost()) {
            $this->success('会员编辑成功！', 'javascript:history.back()');
        }
    }

    /**
     * 修改会员密码
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function pass() {
        $this->applyCsrfToken();
        if ($this->request->isGet()) {
            $this->verify = false;
            $this->_form($this->table, 'pass');
        } else {
            $post = $this->request->post();
            if ($post['password'] !== $post['repassword']) {
                $this->error('两次输入的密码不一致！');
            }
            $salt = substr(sha1(date('r')), rand(0, 17), 22);
            /*
             *  password_hash() – 对密码加密.
                password_verify() – 验证已经加密的密码，检验其hash字串是否一致.
                password_needs_rehash() – 给密码重新加密.
                password_get_info() – 返回加密算法的名称和一些相关信息.
             */
            $pwd = password_hash(md5($post['password'] . $salt), PASSWORD_DEFAULT);

            if (Data::save($this->table, ['id' => $post['id'], 'pwd' => $pwd, 'salt' => $salt], 'id')) {
                $this->success('密码修改成功，下次请使用新密码登录！', '');
            } else {
                $this->error('密码修改失败，请稍候再试！');
            }
        }
    }
}
